home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTCompressed.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  4.9 KB  |  207 lines

  1. #include "HTFormat.h"
  2. #include "HTFile.h"
  3. #include "HTUtils.h"
  4. #include "tcp.h"
  5. #include "HTML.h"
  6. #include "HTMLDTD.h"
  7. #include "HText.h"
  8. #include "HTAlert.h"
  9. #include "HTList.h"
  10. #include "HTInit.h"
  11. #include "HTFWriter.h"
  12. #include "HTPlain.h"
  13. #include "SGML.h"
  14. #include "HTMLGen.h"
  15.  
  16. /* #define TRACE 1 */
  17.  
  18. struct _HTStream 
  19. {
  20.   CONST HTStreamClass*    isa;
  21.   /* ... */
  22. };
  23.  
  24. extern char *mo_tmpnam (void);
  25. extern void application_user_feedback (char *);
  26. extern char *uncompress_program, *gunzip_program;
  27.  
  28. extern void HTFileCopyToText (FILE *fp, HText *text);
  29.  
  30. /* Given a filename of a local compressed file, compress it in place.
  31.  
  32.    We assume that the file does not already have a .Z or .z extension
  33.    at this point -- this is a little weird but it's convenient. */
  34. void HTCompressedFileToFile (char *fnam, int compressed)
  35. {
  36.   char *znam;
  37.   char *cmd;
  38.   
  39.   if (TRACE)
  40.     fprintf 
  41.       (stderr, "[HTCompressedFileToFile] Entered; fnam '%s', compressed %d\n",
  42.        fnam, compressed);
  43.  
  44.   /* Punt if we can't handle it. */
  45.   if (compressed != COMPRESSED_BIGZ && compressed != COMPRESSED_GNUZIP)
  46.     return;
  47.  
  48.   HTProgress ("Preparing to uncompress data.");
  49.   
  50.   znam = (char *)malloc (sizeof (char) * (strlen (fnam) + 8));
  51.  
  52.   /* Either compressed or gzipped. */
  53.   if (compressed == COMPRESSED_BIGZ)
  54.     sprintf (znam, "%s.Z", fnam);
  55.   else
  56.     sprintf (znam, "%s.gz", fnam);
  57.  
  58.   cmd = (char *)malloc (sizeof (char) * (strlen (fnam) + strlen (znam) + 32));
  59. #ifdef _AMIGA
  60.   sprintf (cmd, "rename >NIL: <NIL:  %s %s", fnam, znam);
  61. #else
  62.   sprintf (cmd, "/bin/mv %s %s", fnam, znam);  
  63. #endif
  64.   if (system(cmd) != 0)
  65.     {
  66.       application_user_feedback
  67.         ("Unable to uncompressed compressed data;\nresults may be in error.");
  68.       free (znam);
  69.       free (cmd);
  70.       return;
  71.     }
  72.  
  73.   if (TRACE)
  74.     fprintf (stderr, "[HTCompressedFileToFile] Moved '%s' to '%s'\n",
  75.              fnam, znam);
  76.  
  77.   free (cmd);
  78.   
  79.   if (compressed == COMPRESSED_BIGZ)
  80.     {
  81.       cmd = (char *)malloc (strlen (uncompress_program) + strlen (znam) + 8);
  82.       sprintf (cmd, "%s %s", uncompress_program, znam);
  83.     }
  84.   else
  85.     {
  86.       cmd = (char *)malloc (strlen (gunzip_program) + strlen (znam) + 8);
  87.       sprintf (cmd, "%s %s", gunzip_program, znam);
  88.     }
  89.  
  90.   HTProgress ("Uncompressing data.");
  91.   
  92.   if (system (cmd) != 0)
  93.     {
  94.       application_user_feedback 
  95.         ("Unable to uncompress compressed data;\nresults may be in error.");
  96.       free (cmd);
  97.       return;
  98.     }
  99.  
  100.   HTProgress ("Data uncompressed.");
  101.  
  102.   if (TRACE)
  103.     fprintf 
  104.       (stderr, "[HTCompressedFileToFile] Uncompressed '%s' with command '%s'\n",
  105.        znam, cmd);
  106.   
  107.   free (cmd);
  108.   free (znam);
  109.  
  110.   return;
  111. }
  112.  
  113.  
  114. void HTCompressedHText (HText *text, int compressed, int plain)
  115. {
  116.   char *fnam;
  117.   char *znam;
  118.   char *cmd;
  119.   FILE *fp;
  120.   int rv, size_of_data;
  121.   
  122.   if (TRACE)
  123.     fprintf 
  124.       (stderr, "[HTCompressedHText] Entered; compressed %d\n",
  125.        compressed);
  126.  
  127.   /* Punt if we can't handle it. */
  128.   if (compressed != COMPRESSED_BIGZ && compressed != COMPRESSED_GNUZIP)
  129.     return;
  130.  
  131.   /* Hmmmmmmmmm, I'm not sure why we subtract 1 here, but it is
  132.      indeed working... */
  133.   size_of_data = HText_getTextLength (text) - 1;
  134.  
  135.   if (size_of_data == 0)
  136.     {
  137.       fprintf (stderr, "[HTCompressedHText] size_of_data 0; punting\n");
  138.       return;
  139.     }
  140.   
  141.   fnam = mo_tmpnam ();
  142.   fp = fopen (fnam, "w");
  143.   if (!fp)
  144.     {
  145.       if (TRACE)
  146.         fprintf (stderr, "COULD NOT OPEN TMP FILE '%s'\n", fnam);
  147.       application_user_feedback
  148.         ("Unable to uncompressed compressed data;\nresults may be in error.");
  149.       free (fnam);
  150.       return;
  151.     }
  152.  
  153.   if (TRACE)
  154.     fprintf (stderr, "[HTCmopressedHText] Going to write %d bytes.\n",
  155.              size_of_data);
  156.   rv = fwrite (HText_getText (text), sizeof (char), size_of_data, fp);
  157.   if (rv != size_of_data)
  158.     {
  159.       if (TRACE)
  160.         fprintf (stderr, "ONLY WROTE %d bytes\n", rv);
  161.       application_user_feedback
  162.         ("Unable to write compressed data to local disk;\nresults may be in error.");
  163.     }
  164.   fclose (fp);
  165.  
  166.   if (TRACE)
  167.     fprintf (stderr, "HTCompressedHText: Calling CompressedFileToFile\n");
  168.   HTCompressedFileToFile (fnam, compressed);
  169.  
  170.   HText_clearOutForNewContents (text);
  171.  
  172.   HText_beginAppend (text);
  173.   
  174.   if (plain)
  175.     {
  176.       if (TRACE)
  177.         fprintf (stderr, "[HTCompressedHText] Throwing in PLAINTEXT token...\n");
  178.       HText_appendText(text, "<PLAINTEXT>\n");
  179.     }
  180.  
  181.   fp = fopen (fnam, "r");
  182.   if (!fp)
  183.     {
  184.       if (TRACE)
  185.         fprintf (stderr, "COULD NOT OPEN TMP FILE FOR READING '%s'\n", fnam);
  186.       /* We already get error dialog up above. */
  187.       free (fnam);
  188.       return;
  189.     }
  190.  
  191.   HTFileCopyToText (fp, text);
  192.  
  193.   if (TRACE)
  194.     fprintf (stderr, "[HTCompressedHText] I think we're done...\n");
  195.  
  196.   cmd = (char *)malloc (sizeof (char) * (strlen (fnam) + 32));
  197. #ifdef _AMIGA  
  198.   sprintf (cmd, "delete <NIL: >NIL: %s", fnam);
  199. #else
  200.   sprintf (cmd, "/bin/rm -f %s", fnam);
  201. #endif
  202.   system (cmd);
  203.   free (cmd);
  204.   
  205.   return;
  206. }
  207.